package restfuljobs

import static org.grails.jaxrs.response.Responses.*

import javax.ws.rs.Consumes
import javax.ws.rs.GET
import javax.ws.rs.Produces
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.QueryParam
import javax.ws.rs.POST
import javax.ws.rs.core.Response

@Path('/api/jobListing')
@Consumes(['application/xml','application/json'])
@Produces(['application/xml','application/json'])
class JobListingCollectionResource {

    def jobListingResourceService
    
    @POST
    Response create(JobListing dto, @QueryParam("userId") Long userId) {
        
    	if (userId) {
        	def user = User.get(userId)
        	if (user == null || user.type != User.Type.RECRUITER) {
        	    println "$user is not authorized!"
        		throw new AccessDeniedException(JobListing.class, userId)
        	}
        } else {
        	throw new AccessDeniedException(JobListing.class, userId)        	
        }        
        
        created jobListingResourceService.create(dto)
    }

    @GET
    Response readAll(@QueryParam("jobTitle") String jobTitle,
    	@QueryParam("employer") String employer,
    	@QueryParam("jobCategory") String jobCategory,
    	@QueryParam("location") String location) {
        if (jobTitle || employer || jobCategory || location) {
        	ok jobListingResourceService.search(jobTitle, employer, jobCategory, location)
        } else {
        	ok jobListingResourceService.readAll()
        }
        
    }

    @Path('/{id}')
    JobListingResource getResource(@PathParam('id') Long id) {
        new JobListingResource(jobListingResourceService: jobListingResourceService, id:id)
    }
        
}
